﻿using ByHand.Model;
using System.Configuration;
using Helpers;
using Config;

namespace EmailSender
{
    public class EmailBuilder
    {

        #region template

        private static string _payingtemplate =
            @"<div style='width: 695px; float: left; background: #e3f1fa; font-family: arial; font-size: 12px;margin: 0; padding: 0; font-weight: normal'>
            
            <div style='width: 90%; float: left; margin: 15px 0; padding: 0; margin-left: 5%'>
                <div style='width: 100%; margin: 20px 0; background: white; float: left; padding: 0'>
                    <div style='width: 2%; float: left; margin: 0; padding: 0'>
                        &nbsp;
                    </div>
                    <div style='width: 96%; float: left; margin: 0; padding: 0; margin-left: 2%'>
                        <table cellspacing='0' cellpadding='0' border='0' width='100%'>
                            <tbody>
                                <tr>
                                    <td align='center' valign='bottom'>
                                        <table cellspacing='0' cellpadding='0' width='100%'>
                                            <tbody>
                                                <tr>
                                                    <td bgcolor='' width='90' valign='top' style='padding: 10px; padding-left: 4px'>
                                                        <div style='float: left; text-align: right; overflow: hidden'>
                                                            <a target='_blank' href='http://style.guu.vn' style='text-decoration: none'><span style='font-size: 25px; color: rgb(108, 155, 52); letter-spacing: -2px; text-transform: uppercase;'>Guu<font color='#2e95c2' style='font-weight: bold;padding-left: 5px;'>.vn</font> </span></a>
                                                        </div>
                                                    </td>
                                                    <td bgcolor='' width='' valign='top' style='padding: 10px'>
                                                        <div style='font-family: Arial,Helvetica,sans-serif; float: left; text-align: left;
                                                            color: #0a92cc; font-size: 15px; font-weight: bold; margin-top: 14px'>
                                                            <span>Nền tảng TMĐT dành riêng cho ngành hàng thời trang</span>
                                                        </div>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                                <tr>
                                    <td style='font-family: Arial,Helvetica,sans-serif; font-size: 13px; color: rgb(99,99,99)'>
                                        <table height='30' cellspacing='0' cellpadding='0' border='0' width='100%' style='font-family: Arial,Helvetica,sans-serif'>
                                            <tbody>
                                                <tr style='background: #0a92cc'>
                                                    <td style='text-align:center'>
                                                        <a target='_blank' href=' http://byhand.vn' style='text-decoration: none'>
                                                            <span style='padding: 0 5px!important; color: #0a92cc; text-decoration: none; font-weight: bold;
                                                                padding-right: 5px; font-size: 12px; color: white; text-transform: uppercase'>Giới thiệu</span></a>
                                                    </td>
                                                    <td><span style='color: #ccc'>|</span></td>
                                                    <td style='text-align:center'>
                                                        <a target='_blank' href='http://help.byhand.vn' style='text-decoration: none'><span style='padding: 0 5px!important; color: #0a92cc; text-decoration: none; font-weight: bold;
                                                            padding-right: 5px; font-size: 12px; color: white; text-transform: uppercase'>Trợ giúp</span></a>
                                                    </td>
                                                    <td><span style='color: #ccc'>|</span></td>
                                                    <td style='text-align:center'>
                                                        <a target='_blank' href='mailto:admin@byhand.vn' style='text-decoration: none'>
                                                            <span style='padding: 0 5px!important; color: #0a92cc; text-decoration: none; font-weight: bold;
                                                                padding-right: 5px; font-size: 12px; color: white; text-transform: uppercase'>Liên hệ mở shop</span></a>
                                                    </td>
                                                    <td><span style='color: #ccc'>|</span></td>
                                                    <td style='text-align:center'>
                                                        <a target='_blank' href='{linkcheckorder}' style='text-decoration: none'>
                                                            <span style='padding: 0 5px!important; color: #0a92cc; text-decoration: none; font-weight: bold;
                                                                padding-right: 5px; font-size: 12px; color: white; text-transform: uppercase'>Tra cứu đơn hàng</span></a>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <div>
                            <div style='line-height: 25px; width: 100%; margin: 0; padding: 0; margin-top: 15px'>
                                Chào bạn, <span style='font-size: 14px; font-weight: bold; color: #333333'><a target='_blank' href='mailto:{email}'>{email}</a></span>
                            </div>
                            <div style='line-height: 25px; width: 100%; margin: 0; padding: 0'>
                                Bạn đã đặt hàng thành công tại <a target='_blank' href='http://byhand.vn' style='text-decoration: none'>
                                    <span style='font-size: 12px; color: #0a92cc; text-decoration: underline; font-weight: normal'>
                                        Byhand.vn</span></a>
                            </div>
                        </div>
                        <div style='border-bottom: 1px solid #b1d9ee; margin: 15px 0; clear: left'>
                        </div>
                        <div style='float: left; width: 100%'>
                            <div style='font-size: 14px; font-weight: bold; width: 100%; color: #333333; margin-bottom: 10px'>
                                Thông tin đơn hàng
                            </div>
                            <div style=''>
                                <div style='width: 25%; float: left; line-height: 20px; margin: 0; padding: 0'>Mã đơn hàng:</div>
                                <div style='width: 75%; float: left; line-height: 20px; margin: 0; padding: 0'>
                                    <span style='font-family: arial; font-weight: bold'>{ordercode}</span>
                                </div>
                                <div style='clear: left'>
                                </div>
                                <div style='width: 25%; float: left; line-height: 20px; margin: 0; padding: 0'>Ngày đặt hàng:</div>
                                <div style='width: 75%; float: left; line-height: 20px; margin: 0; padding: 0'>
                                    {date}
                                </div>
                                <div style='clear: left'>
                                </div>
                                <div style='width: 25%; float: left; line-height: 20px; margin: 0; padding: 0'>Dự kiến hàng đến tay bạn:</div>
                                <div style='width: 75%; float: left; line-height: 20px; margin: 0; padding: 0'>
                                    {datepublish}
                                </div>
                            </div>
                        </div>
                        <div style='clear: both'>
                        </div>
                        <div style='margin: 15px 0; clear: left'>
                        </div>
						{products}
                         <div style='margin: 15px 0; clear: left'>
                        </div>
                        <div style='clear: left'>
                        </div>
                       
                        <div style='clear: left'>
                        </div>
                        <div style='margin-top: 5px; clear: left'>
                        </div>
                       
                        <div style='clear: left'>
                        </div>
                        <div style='clear: left'>
                        </div>
                        <div style='border-bottom: 1px solid #b1d9ee; margin: 15px 0'>
                        </div>
                        <div>
                            <div style='font-size: 13px; color: #333333; margin-left: 2px; font-weight: bold'>
                                Cảm ơn bạn đã sử dụng dịch vụ của ByHand.vn</div>
                            <div style='font-family: Arial; font-size: 10px; color: #888888; margin-left: 2px;
                                margin-top: 10px; font-weight: bold'>
                                <span style='color: #999999'>Chú ý: Đây chỉ là email thông báo. Xin vui lòng không phúc
                                    đáp lại. </span>
                            </div>
                        </div>
                        <div style='min-height: 10px; clear: left'>
                        </div>
                    </div>
                    <div style='width: 2%; float: left; margin: 0; padding: 0'>
                        &nbsp;
                    </div>
                </div>
                <div style='clear: left'>
                </div>
                <div style='width: 2%; float: left; margin: 0; padding: 0'>
                    &nbsp;
                </div>
                <div style='margin: 0; padding: 20px 0 20px 0; font-size: 12px; color: #333333; width: 98%;
                    float: left'>
                    <div style='width: 40%; float: left'>
                        <b>Thông tin hỗ trợ </b>
                        <p>
                        </p>
                        <p style='margin: 0; color: #333333'>
                            <b>Hà Nội</b>
                        </p>
                            <p style='margin: 0; padding: 4px 0 4px 0'>
                            Email: <a href='mailto:hotro@byhand.vn'>hotro@byhand.vn</a> (24/7)</p>
                            <p style='margin: 0; padding: 4px 0 4px 0'>
                            Hotline: 0904 782 814. (9h-23h30)</p>
                        <p style='margin: 0; padding: 0 0 4px 0'>
                            Yahoo: <a href='ymsgr:sendIM?byhand_hotro'>byhand_hotro</a> (9h-18h30)</p>
                            <p style='margin: 0; padding: 0 0 4px 0'>
                            Skype: <a href='skype:byhand_hotro?chat'>byhand_hotro</a> (9h-18h30)</p>
                    </div>
                    <div style='width: 60%; float: left'>
                        
                    </div>
                    <div style='clear: left'>
                    </div>
                 
                
                </div>
            </div>
           
        </div>
";

        #endregion
        public static string BuildPayingEmail(string merchantname, Order order)
        {
            var cul = System.Globalization.CultureInfo.GetCultureInfo("vi-VN");
            string producttable =
                @" <table cellspacing='5' cellpadding='2' border='1' width='100%' style='border-collapse: collapse;border: 1px solid #c3c3c3'>
                            <thead>
                                <tr bgcolor='#E6E6E6' valign='middle' style='line-height: 25px; border: 1px solid #c3c3c3'>
                                    <th align='center' width='5%' style='border: 1px solid #c3c3c3'>
                                        STT
                                    </th>
                                    <th align='center' style='border: 1px solid #c3c3c3'>Mã hàng</th>
                                    <th align='center' width='10%' style='border: 1px solid #c3c3c3'>Kích cỡ  </th>

                                    <th align='center' width='15%' style='border: 1px solid #c3c3c3'>
                                       Số lượng
                                    </th>
 <th align='center' width='20%' style='border: 1px solid #c3c3c3'>Giá tiền </th>
                                    <th align='center' width='20%' style='border: 1px solid #c3c3c3'>
                                        Tổng (đ)
                                    </th>
                                </tr>
                            </thead>
                            <tbody>";

            var i = 0;
            int totalprice = 0;
            foreach (var orderDetail in order.OrderDetail)
            {
                i++;
                totalprice += orderDetail.Quantity*order.Price;
                producttable +=
                    string.Format(
                        @" <tr bgcolor='#ffffff' style='line-height: 25px; border: 1px solid #c3c3c3'>
                                    <td align='center' style='border: 1px solid #c3c3c3'>
                                       {0}
                                    </td>
                                    <td align='left' style='font-weight: bold; border: 1px solid #c3c3c3'>{1}</td>
                                    <td align='center' style='border: 1px solid #c3c3c3'>
                                        {2}
                                    </td>
<td align='center' style='border: 1px solid #c3c3c3'>
                                        {3}
                                    </td>
                                    <td align='right' style='color: #990000; font-weight: bold;padding-right: 14px; border: 1px solid #c3c3c3'>
                                        {4}
                                    </td>
                                    <td align='right' style='color: #990000; font-weight: bold; padding-right: 14px;
                                        border: 1px solid #c3c3c3'>
                                        {5}
                                    </td>
                                </tr>",
                                      i, orderDetail.Product.Title, orderDetail.OptionsValue == "undefined" ? "" : orderDetail.OptionsValue.Replace("_", ":").Replace("-", " "), orderDetail.Quantity, (orderDetail.Quantity * order.Price).ToString("#,###", cul.NumberFormat),
                        (orderDetail.Quantity * order.Price).ToString("#,###", cul.NumberFormat));







            }
            producttable += @" <tr style='line-height: 25px; border: 1px solid #c3c3c3'>
                                    <td align='right' style=' border: 1px solid #c3c3c3;color:#ccc' colspan='6'>
                                        (* - Sản phẩm sử dụng mã giảm giá)
                                    </td>
                                </tr>";
            producttable +=
                string.Format(
                    @"<tr style='line-height: 25px; border: 1px solid #c3c3c3'>
                                    <td align='right' style='color: #990000; font-size: 12px; font-weight: bold; border: 1px solid #c3c3c3' colspan='6'>

                                        Tổng tiền &nbsp; = &nbsp; {0} VNĐ
                                    </td>
                                </tr>",
                    StringHelpers.FormatNumber(totalprice.ToString("#,###", cul.NumberFormat)));

            producttable += @"</tbody></table>";
            string template = _payingtemplate;
            template = template.Replace("{email}", order.ToEmail);
            template = template.Replace("{merchantname}", merchantname);
            template = template.Replace("{ordercode}", order.ID.ToString());
            template = template.Replace("{date}", string.Format("{0:dd/MM/yyy}", order.ModifiedDate));
            template = template.Replace("{datepublish}",
                                        string.Format("{0:dd/MM/yyy}", order.ModifiedDate.AddDays(3)));
            template = template.Replace("{linkcheckorder}", UrlMaker.ToRedirectCheckOrder(order.ID.ToString()));

            template = template.Replace("{products}", producttable);
            return template;

        }
    }
}
